Virtual memory gives an application the impression that it has contiguous working memory, while in fact it may be physically fragmented and may even overflow on to disk storage. Systems that use virtual memory may make more efficient use of physical memory, e.g. RAM.
A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource may be a file that is physically present on-disk, but may also be a device, shared memory object or other resource that an operating system may reference through a file handle. Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.
Memory mapping a file may increase I/O performance, especially when used on small files. Accessing memory mapped files may be faster than using direct read and write operations: (1) a system call may be orders of magnitude slower than a simple change of a program's local memory; (2) in most operating systems, the mapped memory region may be the kernel's file cache—no copies need to be created in user space.
Certain application level memory-mapped file operations may perform better than their physical file counterparts. Applications, for example, may access and update data in a file directly, as opposed to seeking from the start of the file or rewriting the entire edited contents to a temporary location. Because a memory-mapped file may be handled internally in pages, linear file access may require disk access only when a new page boundary is crossed and may write larger sections of the file to disk in a single operation.
Memory-mapped files may permit “lazy loading” which uses small amounts of RAM even for very large files. Loading the entire contents of a file (that is significantly larger than the amount of memory available) may cause thrashing as the operating system reads from disk into memory and simultaneously pages from memory back to disk. Memory-mapping may not only bypass the page file completely, but the system may only need to load the smaller page-sized sections as data is being edited.
Memory mapping processes may be handled by a virtual memory manager, which may also be responsible for page files. Memory mapped files may be loaded into memory one entire page at a time. The page size may be selected by the operating system for maximum performance.
Memory mapped file I/O may be chosen for performance reasons. This approach, however, may result in page faults if, for example, a piece of data is not actually loaded by the operating system. Memory mapped file I/O may be slower than standard file I/O. For example, when reading in large files, most of the data may not be cached by the kernel: page faults may occur when reading uncached data.
Memory-mapped files may be used in an operating system process loader. When a process is started, the operating system may use a memory mapped file to bring an executable file, along with any loadable modules, into memory for execution. Some memory-mapping systems use a technique referred to as “demand paging” in which a file is loaded into physical memory in subsets only when that page is actually referenced. In the specific case of executable files, this may permit the operating system to selectively load only those portions of a process image that need to execute.
Memory-mapped files may be used to share memory between multiple processes. In some modern protected mode operating systems, processes are generally not permitted to access memory space that is allocated for use by another process. Two or more applications, however, may simultaneously map a single physical file into memory and access this memory. For example, certain operating systems provide a mechanism for applications to memory-map a shared segment of the system's page file and share data via this section.